<style scoped>
.layui-form>>>.layui-form-select {
    z-index: inherit;
}
</style>

<template>
<div class="company">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>修改田检项</legend>
    </fieldset>

    <form class="layui-form ">
        <input type="hidden" name="guid" placeholder="" autocomplete="off" :value="guid" class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label">田检项名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" :value="pci.name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">田检标准</label>
            <div class="layui-input-block">
                <select id="pcguid" name="plckid" lay-filter="pcguid" lay-verify="required" lay-search="">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" name="code" :value="pci.code" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">必填</label>
            <div class="layui-input-block">
                <select id="required" name="required" v-model="pci.required" required lay-filter="required" lay-verify="required">
                    <option value="1">是</option>
                    <option value="2">否</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">

                <select   id="status"  name="status"  lay-filter="status" required  v-model="pci.status"   lay-search="">
                    <option value="1">启用</option>
                    <option value="0">禁用</option>
                </select>

            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
            </div>
        </div>
        <div style="height: 500px"></div>
    </form>
</div>
</template>

<script>
export default {
    name: 'EditPlantCheckitems',
    props: ['id'],
    data() {
        return {
            guid: this.id,
            pci: {},
        }
    },
    mounted() {
        let that = this;
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            let loader = layer.load();
            //获取回显田检
            $.ajax({
                url: '/farmer/Plantcheckitems!view.action',
                data: {
                    guid: that.guid
                },
                type: "post",
                dataType: "json",
                success: function (res) {
                    if (res.msg != "error") {
                        that.pci = res.result;
                        $.ajax({
                            url: '/farmer/Plantcheck!viewAll.action',
                            data: {},
                            type: "post",
                            dataType: "json",
                            success: function (res) {
                                console.log(res);
                                if (res.info != "error") {
                                    $.each(res.result, function (index, item) {
                                        if (item.name == that.pci.pcname) {
                                            $('#pcguid').append("<option selected  value=" + item.guid + " > " + item.name + "</option>");
                                        } else {
                                            $('#pcguid').append("<option   value=" + item.guid + " >" + item.name + "</option>");
                                        }
                                    });

                                    form.render('select');
                                } else {
                                    that.toast("系统错误");
                                }
                            },
                            complete: function () {
                                layer.close(loader);
                            }
                        });
                        form.render('select');
                    } else {
                        that.toast("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });


            //提交编辑过的田检
            form.on('submit(submit)', function (data) {
                let loader = layer.load();
                $.ajax({
                    url: '/farmer/Plantcheckitems!update.action',
                    data: data.field,
                    type: "post",
                    dataType: "json",
                    success: function (res) {
                        if (res.info == "success") {
                            that.toast("修改成功");
                            that.$parent.switchPage('PlantCheckitems');
                        } else if (res.info == 'noDel') {
                            that.toast("田检项已经被引用，不允许修改！");
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
                return false;
            });
        });
    },
    updated() {
        layui.form.render();
    }
}
</script>
